package com.Dao;

import com.bean.Articletype;
import com.tools.DButil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ArticleTypeDao {
    //添加类别
    public void addtype(String typename) throws SQLException {
        String sql="insert into article_type(typeName)values(?)";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        int row=runner.update(sql,typename);
        if (row==0){
            throw new RuntimeException();//运行时异常，提出了一种新的 null运行时异常的详细信息。原因是没有初始化
        }
    }
    //修改类别
    public void updatetype(Articletype articleType) throws SQLException {
        String sql="update  article_type set typeName=? where id=?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        runner.update(sql,articleType.getTypeName(),articleType.getId());

    }

    //删除类别
    public void deletetype(String typename) throws SQLException {
        String sql="delete from article_type where typename=?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        runner.update(sql,typename);
    }
    //显示所有类别
    public List<Articletype> getAllType() throws SQLException {
        String sql="select * from article_type";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        List<Articletype> res= runner.query(sql,new BeanListHandler<Articletype>(Articletype.class));
        return res;
    }
}
